<template>
  <page-frame>
    <div class="financial-money">
      <div class="toptitle">
        <div class="left">{{baseRuleForm.memberName}}<span>-</span>{{memberNo}}<span>-</span>{{pageData.title}}</div>
        <div class="right">
          <div class="btns">
            <el-button class="lightGreenBtn btnStyle" v-if="currentState=='0'" @click="confirmRecharge">确认充值</el-button>
            <el-button class="lightGreenBtn btnStyle" v-if="currentState=='3'" @click="confirmCharges">确认收费</el-button>
            <el-button class="lightGreenBtn btnStyle" v-if="currentState=='4'" @click="confirmRefunds">确认退费</el-button>
            <el-button class="lightGreenBtn btnStyle" v-if="currentState=='5'" @click="confirmChanges">确认变更</el-button>
            <router-link :to="{name:'financialCenter'}">
              <el-button class="btnStyle">返回</el-button>
            </router-link>
          </div>
        </div>
      </div>
      <div class="borderT"></div>
      <div class="form-css">
        <!--基本信息-->
        <div class="panel-title">
          <el-row :gutter="20">
            <el-col :span="6">
              <div class="grid-content bg-purple">基本信息</div>
            </el-col>
          </el-row>
        </div>
        <el-form :model="baseRuleForm" label-width="110px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="会员姓名">
                <el-input
                  v-model="baseRuleForm.memberName"
                  :disabled="true">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" style="text-align:center;">
              <span @click="accountInfor" style="text-decoration: underline;cursor: pointer">个人账户详情</span>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="床位">
                <el-input
                  v-model="baseRuleForm.bedValue"
                  :disabled="true">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="紧急联系人">
                <el-input
                  v-model="baseRuleForm.contactName"
                  :disabled="true">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="照护等级">
                <el-select v-model="baseRuleForm.lid" :disabled="true" placeholder="请选择照护等级">
                  <el-option
                    v-for="item in gradeList"
                    :label="item.lname"
                    :value="item.lid">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="紧急联系方式">
                <el-input
                  v-model="baseRuleForm.phone"
                  :disabled="true">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div class="panel" v-if="!pageData.isRefill">
          <div class="panel-title">
            <el-row :gutter="20">
              <el-col :span="6">
                <div class="grid-content bg-purple">费用信息</div>
              </el-col>
            </el-row>
          </div>
          <div class="panel-content" style="margin-left: 30px;width: 800px">
            <div class="time">
              <el-col :span="10" v-if="currentState=='3'||currentState=='5'||currentState=='4'">
                <label>{{pageData.time}}:</label>
                <div class="datePicker">
                  <el-date-picker
                    v-model="memberCheckInDate"
                    type="date"
                    @change="selectDate"
                    placeholder="选择日期">
                  </el-date-picker>
                </div>
              </el-col>
              <el-col :span="3" v-if="currentState=='3'">
                <el-button @click="addDeposit">添加押金</el-button>
              </el-col>
              <el-col :span="11" v-if="Deposit">
                <el-input
                  v-model="queryToll.deposit"
                  @blur="deposit"
                  @change="moneyChange">
                </el-input>
              </el-col>
            </div>
            <!--充值、收费、结算-->
            <el-table
              v-if="!pageData.isChange"
              :data="chargeTableData"
              border
              style="width: 100%;margin-top: 25px">
              <el-table-column
                prop="itemName"
                label="费用项目"
                width="">
              </el-table-column>
              <el-table-column
                prop="priceName"
                label="费用标准"
                width="">
              </el-table-column>
              <el-table-column
                prop="paidAmountName"
                label="应收金额"
                v-if="currentState=='3'"
                width="">
              </el-table-column>
              <el-table-column
                prop="paidAmount"
                label="应退金额"
                v-if="currentState=='4'"
                width="">
              </el-table-column>
              <el-table-column
                prop="remarks"
                label="备注"
                v-if="currentState=='3'||currentState=='4'"
                width="">
              </el-table-column>
            </el-table>
            <!--套餐变更费用-->
            <el-table
              v-if="currentChangeStatus=='5'"
              :data="bPackChangeData"
              border
              style="width: 100%;margin-top: 25px">
              <el-table-column
                prop="itemName"
                label="当前套餐类型"
                width="">
              </el-table-column>
              <el-table-column
                prop="price"
                label="套餐价格"
                width="">
              </el-table-column>
            </el-table>
            <el-table
              v-if="currentChangeStatus=='5'"
              :data="aPackChangeData"
              border
              style="width: 100%;margin-top: 25px">
              <el-table-column
                prop="itemName"
                label="更换后的套餐类型"
                width="">
              </el-table-column>
              <el-table-column
                prop="price"
                label="套餐价格"
                width="">
              </el-table-column>
              <el-table-column
                prop="remarks"
                label="备注"
                width="">
              </el-table-column>
            </el-table>
            <!--换床费用-->
            <el-table
              v-if="currentChangeStatus=='4'"
              :data="bChangeBedData"
              border
              style="width: 100%;margin-top: 25px">
              <el-table-column
                prop="itemName"
                label="当前床位类型"
                width="">
              </el-table-column>
              <el-table-column
                prop="usingBedInfo"
                label="床位信息"
                width="">
              </el-table-column>
              <el-table-column
                prop="price"
                label="床位价格"
                width="">
              </el-table-column>
            </el-table>
            <el-table
              v-if="currentChangeStatus=='4'"
              :data="aChangeBedData"
              border
              style="width: 100%;margin-top: 25px">
              <el-table-column
                prop="itemName"
                label="更换后的床位类型"
                width="">
              </el-table-column>
              <el-table-column
                prop="reservedBedInfo"
                label="床位信息"
                width="">
              </el-table-column>
              <el-table-column
                prop="price"
                label="床位价格"
                width="">
              </el-table-column>
              <el-table-column
                prop="remarks"
                label="备注"
                width="">
              </el-table-column>
            </el-table>
          </div>
        </div>
        <!--支付信息-->
        <div class="panel-title">
          <el-row :gutter="20">
            <el-col :span="6">
              <div class="grid-content bg-purple">支付信息</div>
            </el-col>
          </el-row>
        </div>
        <el-form :model="payRuleForm" label-width="110px" class="form-pay">
          <el-row>
            <el-col :span="12" v-if="currentState=='0'">
              <el-form-item label="上月消费总计">
                <el-input
                  v-model="payRuleForm.totalExpense"
                  :disabled="true">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="currentState=='0'">
              <el-form-item label="下次扣费日期">
                <el-input
                  v-model="payRuleForm.nextDeductionDate"
                  :disabled="true">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="账户余额">
                <el-input
                  v-model="payRuleForm.balance"
                  :disabled="true">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="currentState=='4'">
              <el-form-item label="应退金额">
                <el-input
                  v-model="Receivable"
                  :disabled="true">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="currentState=='4'">
              <el-form-item label="实退金额">
                <el-input
                  v-model="querySettlement.paidment"
                  :disabled="true">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="currentState=='3'||currentState=='5'">
              <el-form-item label="应收金额">
                <el-input
                  v-model="Receivable"
                  :disabled="true"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="currentState=='3'||currentState=='5'">
              <el-form-item label="实收金额">
                <el-input
                  v-model="queryToll.paidment"
                  :disabled="true"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="支付方式">
              <el-checkbox-group v-model="checkList" @change="changePayWay">
                <el-checkbox :disabled="changeStatus" label=0 v-if="currentState=='3'||currentState=='5'">余额抵扣</el-checkbox>
                <el-checkbox :disabled="changeStatus" label=1>现金</el-checkbox>
                <el-checkbox :disabled="changeStatus" label=2 v-if="currentState!='4'">刷卡</el-checkbox>
                <el-checkbox :disabled="changeStatus" label=3>转账</el-checkbox>
                <el-checkbox :disabled="changeStatus" label=4>微信</el-checkbox>
                <el-checkbox :disabled="changeStatus" label=5>支付宝</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-row>
          <el-row>
            <el-col :span="12" v-if="payRuleForm.balanceDeductible">
              <el-form-item label="余额抵扣">
                <el-input
                  v-model="payRuleForm.balance"
                  disabled
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="payRuleForm.cash">
              <el-form-item label="现金">
                <el-input
                  v-model="queryCharge.payCash"
                  @change="moneyChange"
                  >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="payRuleForm.payByCard">
              <el-form-item label="刷卡">
                <el-input
                  v-model="queryCharge.payCard"
                  @change="moneyChange"
                  >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="payRuleForm.transferAccounts">
              <el-form-item label="转账">
                <el-input
                  v-model="queryCharge.payTransfer"
                  @change="moneyChange"
                  >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="payRuleForm.wx">
              <el-form-item label="微信">
                <el-input
                  v-model="queryCharge.payWx"
                  @change="moneyChange"
                  >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="payRuleForm.alipay">
              <el-form-item label="支付宝">
                <el-input
                  v-model="queryCharge.payAlipay"
                  @change="moneyChange"
                  >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="currentState=='3'||currentState=='5'">
            <el-col :span="12">
              <el-form-item label="找零">
                <el-input
                  v-model="queryToll.odd"
                  disabled
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" >
              <el-form-item label="存入余额账户">
                <el-switch
                  v-model="queryToll.oddDest"
                  on-value="1"
                  off-value="0"
                  on-text=""
                  off-text="">
                </el-switch>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12" v-if="currentState=='0'">
              <el-form-item label="合计充值金额">
                <el-input
                  v-model="payRuleForm.receivable"
                  :disabled="true"
                  >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="收款人">
                <el-input
                  v-model="payRuleForm.emName"
                  :disabled="true"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <!--确认收费/变更弹框-->
      <el-dialog
        :title="titleName"
        :visible.sync="dialogVisible"
        size="tiny"
        >
        <span>请再次核对<span v-model="typeFeeName">{{typeFeeName}}</span>金额，正确无误再点击「确认」</span>
        <p style="margin-top:10px;">
          <span v-if="currentState!=4">应收金额：</span>
          <span v-if="currentState==4">应退金额：</span>
          <span>{{Receivable}}元</span>
        </p>
        <p>
          <span v-if="currentState!=4">实收金额：</span>
          <span v-if="currentState==4">实退金额：</span>
          <span>{{queryToll.paidment}}元</span>
        </p>
        <!--结算时显示-->
        <p v-if="queryToll.oddDest==1">
          <span v-if="currentState==3||currentState==4||currentState==5">
            <span>转存余额：</span><span>{{queryToll.odd}}元</span>
          </span>
        </p>
        <p v-else>
          <span>转存余额：</span><span>0元</span>
        </p>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <!--结算-->
          <el-button type="primary" @click="confirmRefund" v-if="currentState=='4'">确 认</el-button>
          <!--收费-->
          <el-button type="primary" @click="confirmChargesBox" v-if="currentState=='3'">确 认</el-button>
          <!--换床4/套餐变更5-->
          <el-button type="primary" @click="confirmChange" v-if="currentChangeStatus=='4'||currentChangeStatus=='5'">确 认</el-button>
        </span>
      </el-dialog>
    </div>
  </page-frame>
</template>

<script>
  import financialCenterApi from '../../service/finance/financeCenterApi';
  import commonJs from '../../config/common';
  import comboApi from '../../service/serManagement/comboApi'
  export default {
    data () {
      return {
        index:0,
        memberNo:'',
        titleName:'',
        typeFeeName:'',
        pageData: {
          title:"充值",
          time:"",
          isRefill:true,
          isChange:false
        },
        baseRuleForm:{//基本信息
          memberName:'',
          bedValue:'',
          lid:'',
          contactName:'',
          phone:''
        },
        memberCheckInDate:'',
        dueDate:'',
        payRuleForm:{//支付信息
          totalExpense:0,
          nextDeductionDate:'',
          balance:0,//余额
          receivable: 0,
          balanceDeductible:false,//余额抵扣
          cash:false,//现金
          payByCard:false,//刷卡
          transferAccounts:false,//转账
          wx:false,//微信
          alipay:false,//支付宝
          emName:''
        },
        queryCharge:{//充值提交的数据
          "accountId": 0,//会员账户ID
          "billType": 0,//账单类型
          "payCash": 0,//现金支付
          "payCard": 0,//刷卡支付
          "payTransfer": 0,//转账支付
          "payWx": 0,//微信支付
          "payAlipay": 0,//支付宝支付
          "receivable": 0,
          "type": 0,
        },
        queryToll:{//收费提交的数据
          "accountId": 0,//会员账户ID
          "billId": 0,//账单ID
          "billType":null,
          "receivable": 0,//应收账款
          "paidment": 0,//实收账款
          "deposit":0,//押金
          "payBalance":0,//余额抵扣
          "payCash": 0,//现金支付
          "payCard": 0,//刷卡支付
          "payTransfer": 0,//转账支付
          "payWx": 0,//微信支付
          "payAlipay": 0,//支付宝支付
          "odd": 0,//找零
          "oddDest": 0,//找零去向
        },
        queryPackageChange:{//套餐/床位变更提交的数据
          "accountId": 0,//会员账户ID
          "billId": 0,//账单ID
          "billType":null,
          "receivable": 0,//应收账款
          "paidment": 0,//实收账款
          "payBalance":0,//余额抵扣
          "payCash": 0,//现金支付
          "payCard": 0,//刷卡支付
          "payTransfer": 0,//转账支付
          "payWx": 0,//微信支付
          "payAlipay": 0,//支付宝支付
          "odd": 0,//找零
          "oddDest": 0,//找零去向
        },
        querySettlement:{//结算提交的数据
          "accountId": 0,//会员账户ID
          "billId": 0,//账单ID
          "billType":null,
          "receivable": 0,//应退金额
          "paidment": 0,//实退金额
//          "payBalance":0,//余额抵扣
          "payCash": 0,//现金支付
//          "payCard": 0,//刷卡支付
          "payTransfer": 0,//转账支付
          "payWx": 0,//微信支付
          "payAlipay": 0,//支付宝支付
        },
        Receivable:0,
        Deposit:false,
        chargeTableData:[],//充值、收费、退款
        bPackChangeData:[],//套餐变更前
        aPackChangeData:[],//套餐变更后
        bChangeBedData:[],//换床前
        aChangeBedData:[],//换床后
        bedInfoArr:[],
        checkList: [],
        backUpCheckList:[],
        currentState: 0,
        currentChangeStatus:0,//当前变更状态
        changeStatus:false,
        currentMemberId: null,
        currentActId:null,
        currentBillId:null,
        currentAccountId:null,
        gradeList:[],
        dialogVisible:false,
      }
    },
    methods: {
      init() {
        let self = this;
        self.getGrade();//获取照护等
        if(typeof self.$route.params.rowObj  == "object"){
          self.currentState = self.$route.params.way;
          self.currentAccountId = self.$route.params.rowObj.accountId;
          self.currentMemberId = self.$route.params.rowObj.memberId;
          self.currentActId = self.$route.params.rowObj.actId;
          self.currentBillId = self.$route.params.rowObj.billId;
          self.currentBillType = self.$route.params.rowObj.billType;
          self.currentChangeStatus = self.$route.params.rowObj.billType;
          switch (self.currentState) {
            case 0:
              self.pageData.title = "充值";
              self.pageData.isRefill = true;
              self.pageData.isChange = false;
              this.getAccountRechargeInfo();
              break;
            case 3:
              self.pageData.title = "收费";
              self.pageData.time = "会员入住日期";
              self.pageData.isRefill = false;
              self.pageData.isChange = false;
              this.getAccountBillInfo();
              break;
            case 4:
              self.pageData.title = "结算";
              self.pageData.time = "会员退住日期";
              self.pageData.isRefill = false;
              self.pageData.isChange = false;
              self.getSettlementInfo();
              break;
            case 5:
              self.pageData.title = "费用变更";
              self.pageData.time = "变更/换床日期";
              self.pageData.isRefill = false;
              self.pageData.isChange = true;
              this.getPackageBedChangeInfo();
              break;
          };
        }else{
          self.$router.push({name: 'financialCenter'});
        }
      },
      /**
       * 获取照护等级
       */
      getGrade(){
        let self = this;
        comboApi.getCareGrade().then(function(response){
          self.gradeList = response.body.data;
        })
      },
      /**
       * 会员入住日期改变时
       */
      selectDate(){
        let self = this;
        let d = commonJs.formatDate(self.memberCheckInDate)+'';
        if(self.index==0){//第一次进来，只回显日期，不调接口

        }else{
          //判断日期是否超出范围
          let checkDate = new Date(self.memberCheckInDate);
          checkDate = checkDate.getTime();
          let endDate = new Date(self.dueDate);
          endDate = endDate.getTime();
          if(checkDate>endDate){
            self.$message({
              message:'选择时间超出'+self.dueDate.substring(0,10)+',请重新选择',
              type:'error'
            });
            return;
          }else{

          }

          financialCenterApi.reCalculate(self.currentBillId,d).then(
            (response)=>{
              self.$message({
                message: '修改日期成功',
                type: 'success'
              });
              if(self.currentState==3){
                self.getAccountBillInfo();
              }else if(self.currentState==5){
                self.getPackageBedChangeInfo();
              }else if(self.currentState==4){
                self.getSettlementInfo();
              }
            }
          );
        }
        self.index++;
      },
      /**
       * 点击添加押金
       */
      addDeposit(){
        this.Deposit = true;
      },
      /**
       * 将押金添加到表格里面
       */
      deposit(){
        let self = this;
        let des = {
          itemName : '押金',
          price:'',
          paidAmount :self.queryToll.deposit,
          paidAmountName:self.queryToll.deposit+'元',
          remarks:''
        };
        //判断是否为多次添加
        let len = self.chargeTableData.length;
        if(self.chargeTableData[len-1].itemName=='押金'){//重复添加
          //先删除旧的
          self.chargeTableData.splice(len-1,1);
        }
        self.chargeTableData.push(des);
      },
      /**
       * 确认充值
       */
      confirmRecharge(){
        let self = this;
        self.queryCharge.accountId = self.currentAccountId;
        self.queryCharge.billType = self.$route.params.rowObj.billType;
        self.queryCharge.type = 0;//收入
        self.queryCharge.receivable = self.payRuleForm.receivable;
        self.$confirm('请再次核对充值金额，正确无误再点击「确认」', '确认收费', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          financialCenterApi.payAccountBill(self.queryCharge).then(
            (response)=>{
              self.$message({
                type: 'success',
                message: '充值成功!'
              });
              self.$router.push({name:'financialCenter'});
            }
          );
        }).catch(() => {
          self.$message({
            type: 'info',
            message: '已取消充值'
          });
        });
      },
      /**
       * 确认收费
       */
      confirmCharges(){
        this.dialogVisible = true;
        this.titleName = '确认收费';
        this.typeFeeName = '收费';
      },
      /**
       * 确认收费弹框(确认按钮)
       */
      confirmChargesBox(){
        let self = this;
        self.dialogVisible = false;
        self.queryToll.accountId = self.currentAccountId;
        self.queryToll.billId = self.currentBillId;
        self.queryToll.receivable = self.Receivable;
        if(self.queryToll.oddDest==1){
          self.queryToll.payBalance = self.payRuleForm.balance;
        }else{
          self.queryToll.payBalance = 0;
        }
        self.queryToll.payCash = self.queryCharge.payCash;
        self.queryToll.payCard = self.queryCharge.payCard;
        self.queryToll.payTransfer = self.queryCharge.payTransfer;
        self.queryToll.payWx = self.queryCharge.payWx;
        self.queryToll.payAlipay = self.queryCharge.payAlipay;

        if(self.Receivable>self.queryToll.paidment){
          self.$message({
            message: '实收金额<应收金额',
            type: 'warning'
          });
          return false;
        }
        financialCenterApi.payAccountBill(self.queryToll).then(
          (response)=>{
            self.$message({
              message: '收费成功',
              type: 'success'
            });
            self.$router.push({name:'financialCenter'});
          }
        );
      },
      /**
       * 确认结算
       */
      confirmRefunds(){
        this.dialogVisible = true;
        this.titleName = '确认结算';
        this.typeFeeName = '结算';
      },
      /**
       * 确认结算(弹框)
       */
      confirmRefund(){
        let self = this;
        self.dialogVisible = false;
        self.querySettlement.accountId = self.currentAccountId;
        self.querySettlement.billId = self.currentBillId;
        self.querySettlement.receivable = self.querySettlement.receivable;//应退金额
        self.querySettlement.paidment = self.querySettlement.paidment;//实退金额
        if(this.Receivable<0){
          self.querySettlement.payCash = -self.queryCharge.payCash;//现金
          self.querySettlement.payTransfer = -self.queryCharge.payTransfer;//转账
          self.querySettlement.payWx = -self.queryCharge.payWx;//微信
          self.querySettlement.payAlipay = -self.queryCharge.payAlipay;//支付宝
        }else{
          self.querySettlement.payCash = self.queryCharge.payCash;//现金
          self.querySettlement.payTransfer = self.queryCharge.payTransfer;//转账
          self.querySettlement.payWx = self.queryCharge.payWx;//微信
          self.querySettlement.payAlipay = self.queryCharge.payAlipay;//支付宝
        }

        financialCenterApi.settlementBill(self.querySettlement).then(
          (response)=>{
            this.$message({
              type: 'success',
              message: '结算成功'
            });
            self.$router.push({name:'financialCenter'});
          }
        );
      },
      /**
       * 确认变更
       */
      confirmChanges(){
        this.dialogVisible = true;
        this.titleName = '确认变更';
        this.typeFeeName = '变更';
      },
      /**
       * 确认变更(弹框)
       */
      confirmChange(){
        let self = this;
        self.dialogVisible = false;
        self.queryPackageChange.accountId = self.currentAccountId;
        self.queryPackageChange.billId = self.currentBillId;
        self.queryPackageChange.billType = self.currentBillType;
        self.queryPackageChange.receivable = self.Receivable;
        self.queryPackageChange.paidment = self.queryToll.paidment;
        if(self.Receivable<0){
          self.queryPackageChange.payBalance = 0;
        }else{
          //判断是否勾选余额抵扣
          for(let i=0;i<self.checkList.length;i++){
            if(self.checkList[i]==0){//勾选余额抵扣
              if(self.queryToll.oddDest==1&&self.checkList[i]==0){
                self.queryPackageChange.payBalance = self.payRuleForm.balance;
              }else{
                self.queryPackageChange.payBalance = 0;
              }
              break;
            }
          }
//          self.queryPackageChange.payBalance = self.payRuleForm.balance;
        }
        self.queryPackageChange.payCash = self.queryCharge.payCash;
        self.queryPackageChange.payCard = self.queryCharge.payCard;
        self.queryPackageChange.payTransfer = self.queryCharge.payTransfer;
        self.queryPackageChange.payWx = self.queryCharge.payWx;
        self.queryPackageChange.payAlipay = self.queryCharge.payAlipay;
        self.queryPackageChange.oddDest = self.queryToll.oddDest;
        self.queryPackageChange.odd = self.queryToll.odd;

//        //判断是否勾选余额抵扣
//        for(let i=0;i<self.checkList.length;i++){
//          if(self.checkList[i]==0){//勾选余额抵扣
//            if(self.queryToll.oddDest==1&&self.checkList[i]==0){
//              self.queryPackageChange.payBalance = self.payRuleForm.balance;
//            }else{
//              self.queryPackageChange.payBalance = 0;
//            }
//            break;
//          }
//        }

        if(self.Receivable>self.queryToll.paidment){
          self.$message({
            message: '实收金额<应收金额',
            type: 'warning'
          });
          return false;
        }

        financialCenterApi.payAccountBill(self.queryPackageChange).then(
          (response)=>{
            self.$message({
              message: '更改成功',
              type: 'success'
            });
            self.$router.push({name:'financialCenter'});
          }
        );
      },
      /**
       * 充值方式改变时
       */
      changePayWay(event){
        let self = this;
        if(self.backUpCheckList.length==0){//第一次点击(备份数组为空)
          if(self.checkList[0]==0){//balanceDeductible
            self.payRuleForm.balanceDeductible = true;
          }else if(self.checkList[0]==1){//cash
            self.payRuleForm.cash = true;
          }else if(self.checkList[0]==2){//payByCard
            self.payRuleForm.payByCard = true;
          }else if(self.checkList[0]==3){//transferAccounts
            self.payRuleForm.transferAccounts = true;
          }else if(self.checkList[0]==4){//wx
            self.payRuleForm.wx = true;
          }else if(self.checkList[0]==5){//alipay
            self.payRuleForm.alipay = true;
          }
        }else{
          //当数组增加,增加的id肯定是在数组的最后
         if(self.checkList.length>self.backUpCheckList.length){//数组增加,并清空数据
           if(self.checkList[self.checkList.length-1]==0){
             self.payRuleForm.balanceDeductible = true;//应收金额不能清空
           }else if(self.checkList[self.checkList.length-1]==1){//cash
             self.queryCharge.payCash = 0;
             self.payRuleForm.cash = true;
           }else if(self.checkList[self.checkList.length-1]==2){//payByCard
             self.queryCharge.payCard = 0;
             self.payRuleForm.payByCard = true;
           }else if(self.checkList[self.checkList.length-1]==3){//transferAccounts
             self.queryCharge.payTransfer = 0;
             self.payRuleForm.transferAccounts = true;
           }else if(self.checkList[self.checkList.length-1]==4){//wx
             self.queryCharge.payWx = 0;
             self.payRuleForm.wx = true;
           }else if(self.checkList[self.checkList.length-1]==5){//alipay
             self.queryCharge.payAlipay = 0;
             self.payRuleForm.alipay = true;
           }
         }else if(self.checkList.length<self.backUpCheckList.length){//数组减小
           if(self.checkList.length==0){
             if(self.backUpCheckList[0]==0){
               self.payRuleForm.balanceDeductible = false;
             }else if(self.backUpCheckList[0]==1){//cash
               self.queryCharge.payCash = 0;
               self.payRuleForm.cash = false;
             }else if(self.backUpCheckList[0]==2){//payByCard
               self.queryCharge.payCard = 0;
               self.payRuleForm.payByCard = false;
             }else if(self.backUpCheckList[0]==3){//transferAccounts
               self.queryCharge.payTransfer = 0;
               self.payRuleForm.transferAccounts = false;
             }else if(self.backUpCheckList[0]==4){//wx
               self.queryCharge.payWx = 0;
               self.payRuleForm.wx = false;
             }else if(self.backUpCheckList[0]==5){//alipay
               self.queryCharge.payAlipay = 0;
               self.payRuleForm.alipay = false;
             }
           }else{
             outerloop://命名外圈语句
               for(let i=0;i<self.backUpCheckList.length;i++){
                 innerloop://命名内圈语句
                   for(let k=0;k<self.checkList.length;k++){
                     if(self.backUpCheckList[i]!=self.checkList[i]){
                       if(self.backUpCheckList[i]==0){
                         self.payRuleForm.balanceDeductible = false;
                       }else if(self.backUpCheckList[i]==1){//cash
                         self.queryCharge.payCash = 0;
                         self.payRuleForm.cash = false;
                       }else if(self.backUpCheckList[i]==2){//payByCard
                         self.queryCharge.payCard = 0;
                         self.payRuleForm.payByCard = false;
                       }else if(self.backUpCheckList[i]==3){//transferAccounts
                         self.queryCharge.payTransfer = 0;
                         self.payRuleForm.transferAccounts = false;
                       }else if(self.backUpCheckList[i]==4){//wx
                         self.queryCharge.payWx = 0;
                         self.payRuleForm.wx = false;
                       }else if(self.backUpCheckList[i]==5){//alipay
                         self.queryCharge.payAlipay = 0;
                         self.payRuleForm.alipay = false;
                       }
                       break outerloop;
                     }
                   }
               }
           }
         }
        }

        if(this.currentState==5||this.currentState==3){//变更
          if(self.checkList.length==0){
            self.queryToll.oddDest = '0';
          }else{
            for(let k=0;k<self.checkList.length;k++){
              if(self.checkList[k]==0){//选中余额抵扣
                self.queryToll.oddDest = '1';//默认打开存入余额账户
                break;
              }else{
                self.queryToll.oddDest = '0';
              }
            }
          }
        }
        self.moneyChange();
        self.backUpCheckList = self.checkList;//备份数据
      },

      /**
       * 当金额数目改变时
       */
      moneyChange(){
        if(this.currentState==0){//充值
          this.payRuleForm.receivable =
            this.toNumber(this.queryCharge.payCash)
            +this.toNumber(this.queryCharge.payCard)
            +this.toNumber(this.queryCharge.payTransfer)
            +this.toNumber(this.queryCharge.payWx)
            +this.toNumber(this.queryCharge.payAlipay);
        }else if(this.currentState==3||this.currentState==5||this.currentState==4){//收费、变更
          if(this.currentState==3){//收费(包含押金)
            //应付金额=返回的金额+押金
            if(this.queryToll.deposit==''){
              this.Receivable =this.toNumber(this.queryToll.receivable);
            }else{
              this.Receivable =
                this.toNumber(this.queryToll.receivable)
                +this.toNumber(this.queryToll.deposit);
            }
          }else if(this.currentState==5){//变更(不包含押金)

          }

          if(this.currentState==4){//结算 实退金额
            this.querySettlement.paidment =
              this.toNumber(this.queryCharge.payCash)
              + this.toNumber(this.queryCharge.payTransfer)
              + this.toNumber(this.queryCharge.payWx)
              + this.toNumber(this.queryCharge.payAlipay);
            if(this.Receivable<0){//如果结算的时候会员有欠款(实退金额为负值)
              this.querySettlement.paidment = -this.querySettlement.paidment;
            }else{

            }
          }

          //实付金额
          if(this.payRuleForm.balanceDeductible){//显示余额抵扣
            this.queryToll.paidment =
              this.toNumber(this.queryCharge.payCash)
              +this.toNumber(this.queryCharge.payCard)
              +this.toNumber(this.queryCharge.payTransfer)
              +this.toNumber(this.queryCharge.payWx)
              +this.toNumber(this.queryCharge.payAlipay)
              +this.toNumber(this.payRuleForm.balance);
          }else{//不显示余额抵扣
            this.queryToll.paidment =
              this.toNumber(this.queryCharge.payCash)
              +this.toNumber(this.queryCharge.payCard)
              +this.toNumber(this.queryCharge.payTransfer)
              +this.toNumber(this.queryCharge.payWx)
              +this.toNumber(this.queryCharge.payAlipay);
          }
          //找零
          if(this.queryToll.paidment>this.Receivable){
            this.queryToll.odd = this.queryToll.paidment - this.Receivable;
          }else if(this.Receivable<0){//为负值
            this.queryToll.odd = -this.Receivable;
          }else{
            this.queryToll.odd = 0;
          }
          this.queryToll.odd = parseFloat(this.queryToll.odd).toFixed(2);
        }
      },
      /**
       * 保留2位小数
       */
      toNumber(val){
        return Math.round(val*100)/100;
      },
      /**
       * 充值信息查询
       */
      getAccountRechargeInfo(){
        let self = this;
        financialCenterApi.getAccountRecharge(self.currentMemberId).then(
          (response)=>{
            //基本信息
            self.memberNo = response.data.data.memberDTO.memberNo;
            self.baseRuleForm.memberName = response.data.data.memberDTO.memberName;
            self.baseRuleForm.lid = response.data.data.memberDTO.lid;
            self.baseRuleForm.bedValue = response.data.data.memberDTO.bedNo;
            self.baseRuleForm.contactName = response.data.data.memberContactDTO.contactName;
            self.baseRuleForm.phone = response.data.data.memberContactDTO.phone;
            //支付信息
            self.payRuleForm.totalExpense = response.data.data.accountDTO.totalExpense;
            self.payRuleForm.nextDeductionDate = response.data.data.accountDTO.nextDeductionDate;
            self.payRuleForm.balance = response.data.data.accountDTO.balance;
            self.payRuleForm.emName = response.data.data.accountBillDTO.emName;
          });
      },
      /**
       * 收费详细信息查询
       */
      getAccountBillInfo(){
        let self = this;
        financialCenterApi.getCostInfo(self.currentMemberId,self.currentBillId).then(
          (response)=>{
            self.queryToll.billType = response.data.data.accountBillDTO.billType;
            //基本信息
            self.memberNo = response.data.data.memberDTO.memberNo;
            self.baseRuleForm.memberName = response.data.data.memberDTO.memberName;
            self.baseRuleForm.lid = response.data.data.memberDTO.lid;
            self.baseRuleForm.bedValue = response.data.data.memberDTO.bedNo;
            self.baseRuleForm.contactName = response.data.data.memberContactDTO.contactName;
            self.baseRuleForm.phone = response.data.data.memberContactDTO.phone;
            self.memberCheckInDate = response.data.data.accountBillDTO.changeDate;
            self.dueDate = response.data.data.accountBillDTO.dueDate;
            //收费信息
            self.chargeTableData = response.data.data.accountExpenseItemDTOList;
            //添加收费信息单位
            for(let i=0;i<self.chargeTableData.length;i++){
              if(self.chargeTableData[i].unit==0){//0:元/月
                self.chargeTableData[i].priceName =self.chargeTableData[i].price + '元/月';
              }else if(self.chargeTableData[i].unit==1){//元
                self.chargeTableData[i].priceName =self.chargeTableData[i].price + '元';
              }
            }
            for(let i=0;i<self.chargeTableData.length;i++){
              self.chargeTableData[i].paidAmountName = self.chargeTableData[i].paidAmount+"元";
              //添加备注
              self.chargeTableData[i].remarks = commonJs.formateDateToTime(self.chargeTableData[i].changeDate)
                +'-'+commonJs.formateDateToTime(self.chargeTableData[i].dueDate)
                +'('+self.chargeTableData[i].typeName+')';
            }

            //支付信息
            self.payRuleForm.totalExpense = response.data.data.accountDTO.totalExpense;
            self.payRuleForm.nextDeductionDate = response.data.data.accountDTO.nextDeductionDate;
            self.payRuleForm.balance = response.data.data.accountDTO.balance;
            self.payRuleForm.emName = response.data.data.accountBillDTO.emName;
            self.Receivable = response.data.data.accountBillDTO.receivable;
            self.queryToll.receivable = response.data.data.accountBillDTO.receivable;
          }
        );
      },
      /**
       * 换床及套餐变更信息查询
       */
      getPackageBedChangeInfo(){
        let self = this;
        let viewBed = 0;//是否查看床位  0:查看套餐变更 1:查看床位
        if(self.currentChangeStatus=='5'){
          viewBed = 0;
        }else if(self.currentChangeStatus=='4'){
          viewBed = 1;
        }
        self.getBedInfoByMemberId();
        financialCenterApi.getPackageBedChangesInfo(self.currentMemberId,self.currentActId,viewBed).then(
          (response)=>{
            self.queryToll.billType = response.data.data.accountBillDTO.billType;
            //基本信息
            self.memberNo = response.data.data.memberDTO.memberNo;
            self.baseRuleForm.memberName = response.data.data.memberDTO.memberName;
            self.baseRuleForm.lid = response.data.data.memberDTO.lid;
            self.baseRuleForm.bedValue = response.data.data.memberDTO.bedNo;
            self.baseRuleForm.contactName = response.data.data.memberContactDTO.contactName;
            self.baseRuleForm.phone = response.data.data.memberContactDTO.phone;
            self.memberCheckInDate = response.data.data.accountBillDTO.changeDate;
            self.dueDate = response.data.data.accountBillDTO.dueDate;
            //套餐收费信息
            if(self.currentChangeStatus=='5'){//套餐变更
              self.bPackChangeData = response.data.data.currentExpenseItemDTOList;
              self.aPackChangeData = response.data.data.accountExpenseItemDTOList;
              //套餐添加备注
              for(let i=0;i<self.aPackChangeData.length;i++){
                self.aPackChangeData[i].remarks = commonJs.formateDateToTime(self.memberCheckInDate)
                  +'-'+
                  commonJs.formateDateToTime(self.dueDate)
                  +'('+self.aPackChangeData[i].typeName+')';
              }
            }else if(self.currentChangeStatus=='4'){//换床变更
              self.bChangeBedData = response.data.data.currentExpenseItemDTOList;
              for(let i=0;i<self.bChangeBedData.length;i++){
                self.bChangeBedData[i].usingBedInfo = self.bedInfoArr[0].bedNo;
              }
              self.aChangeBedData = response.data.data.accountExpenseItemDTOList;
              for(let i=0;i<self.aChangeBedData.length;i++){
                self.aChangeBedData[i].reservedBedInfo = self.bedInfoArr[1].bedNo;
                self.aChangeBedData[i].remarks = commonJs.formateDateToTime(self.memberCheckInDate)
                  +'-'+
                  commonJs.formateDateToTime(self.dueDate)
                  +'('+self.aChangeBedData[i].typeName+')';
              }
            }
            //支付信息
            self.payRuleForm.totalExpense = response.data.data.accountDTO.totalExpense;
            self.payRuleForm.nextDeductionDate = response.data.data.accountDTO.nextDeductionDate;
            self.payRuleForm.balance = response.data.data.accountDTO.balance;
            self.payRuleForm.emName = response.data.data.accountBillDTO.emName;
            self.Receivable = response.data.data.accountBillDTO.receivable;
            self.queryPackageChange.receivable = response.data.data.accountBillDTO.receivable;
            if(response.data.data.accountBillDTO.receivable<0){
              self.changeStatus = true;
              self.queryToll.oddDest = '1';
              self.moneyChange();
            }else{
              self.changeStatus = false;
              self.queryToll.oddDest = '0';
            }
          }
        );
      },

      /**
       * 根据memberI获取床位信息
       */
      getBedInfoByMemberId(){
        let self = this;
        financialCenterApi.getBedInfo(self.currentMemberId).then(
          (response)=>{
            self.bedInfoArr = response.data.data;
          }
        );
      },

      /**
       * 结算详情
       */
      getSettlementInfo(){
        let self = this;
        financialCenterApi.getSettlementDetails(self.currentMemberId,self.currentBillId).then(
          (response)=>{
            self.queryToll.billType = response.data.data.accountBillDTO.billType;
            //基本信息
            self.memberNo = response.data.data.memberDTO.memberNo;
            self.baseRuleForm.memberName = response.data.data.memberDTO.memberName;
            self.baseRuleForm.lid = response.data.data.memberDTO.lid;
            self.baseRuleForm.bedValue = response.data.data.memberDTO.bedNo;
            self.baseRuleForm.contactName = response.data.data.memberContactDTO.contactName;
            self.baseRuleForm.phone = response.data.data.memberContactDTO.phone;
            self.memberCheckInDate = response.data.data.accountBillDTO.changeDate;
            self.dueDate = response.data.data.accountBillDTO.dueDate;
            //结算信息
            self.chargeTableData = response.data.data.accountExpenseItemDTOList;
            //添加结算信息单位
            for(let i=0;i<self.chargeTableData.length;i++){
              if(self.chargeTableData[i].unit==0){//0:元/月
                self.chargeTableData[i].priceName =self.chargeTableData[i].price + '元/月';
              }else if(self.chargeTableData[i].unit==1){//元
                self.chargeTableData[i].priceName =self.chargeTableData[i].price + '元';
              }
            }
            for(let i=0;i<self.chargeTableData.length;i++){
              self.chargeTableData[i].paidAmountName = self.chargeTableData[i].paidAmount+"元";
              if(self.chargeTableData[i].typeName=='押金'){
                self.chargeTableData[i].remarks = '';
              }else{
                self.chargeTableData[i].remarks = commonJs.formateDateToTime(self.chargeTableData[i].changeDate)
                  +'-'+commonJs.formateDateToTime(self.chargeTableData[i].dueDate)
                  +'('+self.chargeTableData[i].typeName+')';
              }
            }

            //支付信息
            self.payRuleForm.balance = response.data.data.accountDTO.balance;//余额
            self.Receivable = response.data.data.accountBillDTO.receivable+self.payRuleForm.balance;//应退金额
            self.querySettlement.receivable = response.data.data.accountBillDTO.receivable;//应退金额
            self.payRuleForm.emName = response.data.data.accountBillDTO.emName;
          }
        );
      },
      /**
       * 个人账户详情
       */
      accountInfor(){
        this.$router.push({name: 'accountDetails', params: {memberId: this.currentMemberId,accountId:this.currentAccountId}});
      },
    },

    mounted: function () {
      this.init();
    },
  }
</script>
<style>
  .financial-money .form-css {

  }

  .financial-money .el-table .cell, .el-table th > div {
    text-align: center;
  }

  .financial-money .form-pay {
    margin-top: 30px;
  }

  .financial-money .time {
    margin: 24px 0 15px 0;
    overflow: hidden;
  }

  .financial-money .time .datePicker {
    display:inline-block;
  }
</style>
